import { createReducer } from './util';
import * as LogAction from '../actions/log';
import _ from 'lodash';

export const logs = createReducer(on => {
  on(LogAction.getLog)
    .completed((state, action) => {
      const { logKey } = action.meta;
      const { status, content } = action.payload;
      let newState;
      if (status === 0) {
        newState = action.payload;
      } else {
        const oldState = state[logKey] || {};
        const oldContent = oldState.content || '';
        newState = {
          content: oldContent + content,
          status
        };
      }
      return Object.assign({}, state, { [logKey]: newState });
    });
  on(LogAction.clearLog)
    .completed((state, action) => {
      return _.omit(state, action.payload);
    });
}, {});
